package com.jams.goodox.activity.serve.park;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jams.goodox.R;
import com.jams.goodox.activity.ChoosePayActivity;
import com.jams.goodox.activity.serve.ServeOrderBaseDetailActivity;
import com.jams.goodox.constant.Constants;
import com.jams.goodox.entity.ParkOrderDetailBodyBean;
import com.jams.goodox.entity.PayInfo;
import com.jams.goodox.entity.ServeOrderDetailBean;
import com.jams.goodox.entity.resp.ParkOrderDetailResp;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.ToastUtil;

import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * 停车申请订单详情页
 * Created by guozhen.hou on 2017/7/11.
 */

public class ParkOrderDetailActivity extends ServeOrderBaseDetailActivity {

    private boolean isChangePayStatus = false;//是否取消订单
    private String orderId;
    private ServeOrderDetailBean orderDetailBean;
    private boolean isPersonPark;

    @Override
    protected void getOrderDetail() {
        showLoading();
        if (TextUtils.isEmpty(orderId)) orderId = getIntent().getStringExtra("OrderId");
        isPersonPark=getIntent().getBooleanExtra("isPersonPark",false);
        enqueue(createCallService(ServiceService.class).getParkOrderDetail(orderId), new RetrofitCallbackListener<ParkOrderDetailResp>() {
            @Override
            public void onSuccess(Call<ParkOrderDetailResp> call, ParkOrderDetailResp result) {
                misLoading();
                if(result.getData()==null)return;
                orderDetailBean=result.getData().transform();
                orderDetailBean.setPersonOrder(isPersonPark);
                setUiData(orderDetailBean);
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    @Override
    protected void doActivityBack() {
        if (isChangePayStatus) setResult(Constants.RESULT_OK_SIMPLE);
        finish();
    }

    @Override
    protected void setDetailData(ServeOrderDetailBean serveOrderDetailBean) {
        frameServeDetail.removeAllViews();
        if (serveOrderDetailBean.getServeDetail() instanceof ParkOrderDetailBodyBean) {
            View view = getLayoutInflater().inflate(R.layout.park_order_detail_body, null);
            frameServeDetail.addView(view);
            TextView txCarNumber=ButterKnife.findById(view,R.id.tx_car_number);
            TextView txPriceUnit=ButterKnife.findById(view,R.id.tx_price_unit);
            TextView txNumber=ButterKnife.findById(view,R.id.tx_number);
            TextView txUseTime=ButterKnife.findById(view,R.id.tx_use_time);
            TextView txRemark=ButterKnife.findById(view,R.id.tx_remark);
            TextView txServiceName=ButterKnife.findById(view,R.id.tx_service_name);
            ParkOrderDetailBodyBean parkOrderDetailBodyBean=(ParkOrderDetailBodyBean)serveOrderDetailBean.getServeDetail();
            txCarNumber.setText(parkOrderDetailBodyBean.getCarNumber());
            txPriceUnit.setText(parkOrderDetailBodyBean.getPriceUnit());
            txNumber.setText(String.format("x%s",parkOrderDetailBodyBean.getBuyNumber()));
            txUseTime.setText(parkOrderDetailBodyBean.getUseDateTime());
            txRemark.setText(String.format("备注信息：%s",parkOrderDetailBodyBean.getRemark()));
            txServiceName.setText(parkOrderDetailBodyBean.getServiceName());
        }
    }

    @Override
    protected void cancelOrder() {
        showLoading();
        enqueue(createCallService(ServiceService.class).cancelParkOrder(orderId), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                isChangePayStatus=true;
                misLoading();
                getOrderDetail();
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    @Override
    protected void payOrder() {
        PayInfo payInfo=(PayInfo)getIntent().getSerializableExtra("payInfo");
        if(payInfo==null||orderDetailBean==null)return;
        Intent intent = new Intent(mContext, ChoosePayActivity.class);
        if (orderDetailBean.getRedPacketMoney()!=0)payInfo.setRedPacketMoney(orderDetailBean.getRedPacketMoney());
        if(isPersonPark) {//个人订单1 企业订单2
            payInfo.setOrderCreateClassfiy(1);
        }else {
            payInfo.setOrderCreateClassfiy(2);
        }
        payInfo.setPayType(orderDetailBean.getChoosePayTypeList());
        payInfo.setPaySubCode(Constants.PARKING_CODE);//该字段用于余额支付，支付成功返回
        payInfo.setServerName("停车申请");
        payInfo.setOrderAllMoney(orderDetailBean.getTotalMoney());//传递的是订单总额
        payInfo.setThirdPayConfigType(Constants.PayType.Type_Parking);
        intent.putExtra("payInfo", payInfo);
        startActivity(intent);
    }
}
